home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-09-26 | 10.0 KB | 431 lines | [TEXT/CWIE] |
- // COPYRIGHT 1994 A.D. Software, All rights reserved
-
- // OOFILE database queries
-
- #include "oofquery.hpp"
- #include "oof3.hpp"
- #include <assert.h>
-
- // -------------------------------------------------------
- // d b Q u e r y C l a u s e
- // -------------------------------------------------------
- dbQueryBinaryCombo dbQueryClause::operator&(dbQueryClause& rhs) const
- {
- return dbQueryBinaryCombo(this, dbQueryClause::and, &rhs);
- }
-
-
- dbQueryBinaryCombo dbQueryClause::operator&&(dbQueryClause& rhs) const
- {
- return dbQueryBinaryCombo(this, dbQueryClause::and, &rhs);
- }
-
-
- dbQueryBinaryCombo dbQueryClause::operator|(dbQueryClause& rhs) const
- {
- return dbQueryBinaryCombo(this, dbQueryClause::or, &rhs);
- }
-
-
- dbQueryBinaryCombo dbQueryClause::operator||(dbQueryClause& rhs) const
- {
- return dbQueryBinaryCombo(this, dbQueryClause::or, &rhs);
- }
-
-
-
- // -------------------------------------------------------
- // d b Q u e r y B i n a r y
- // -------------------------------------------------------
- dbQueryBinary::dbQueryBinary(dbQueryBinary& rhs)
- {
- mLhs = rhs.mLhs;
- mRhs = rhs.mRhs;
- mBinOp = rhs.mBinOp;
- rhs.mLhs = 0;
- rhs.mRhs = 0;
- }
-
-
- dbQueryBinary::~dbQueryBinary()
- {
- delete mLhs;
- delete mRhs;
- }
-
-
- dbQueryBinary& dbQueryBinary::operator=(dbQueryBinary& rhs)
- {
- assert(0); // should never be called
- return *this;
- };
-
-
- dbQueryClause::QueryClauseTypes dbQueryBinary::queryClauseType() const
- {
- if (mRhs->queryClauseType()==atomicfield)
- return binaryfieldTofield;
- else
- return binaryfieldToLiteral;
- }
-
-
- dbQueryLiteralStr* dbQueryBinary::literalStrClause() const
- {
- if (mRhs->queryClauseType()==atomicLiteralStr)
- return (dbQueryLiteralStr*) mRhs; // safe downcast
- else
- return 0;
- }
-
-
- dbQueryLiteral* dbQueryBinary::literalClause() const
- {
- // NOTE the following assumes that logic to handle binary logical clauses is a superset of
- // that handling strings, and if the difference is important we have tried literalStrClause()
- // BEFORE trying this routine
- if ((mRhs->queryClauseType()==atomicLiteral) || (mRhs->queryClauseType()==atomicLiteralStr))
- return (dbQueryLiteral*) mRhs; // safe downcast, actually to a parent of the exact type (a dbQueryLiteralLong etc.)
- else
- return 0;
- }
-
-
-
- // -------------------------------------------------------
- // d b Q u e r y B i n a r y C o m b o
- // -------------------------------------------------------
- dbQueryBinaryCombo::~dbQueryBinaryCombo()
- {
- delete[] mTempKeyStoreToDispose;
- }
-
-
- dbQueryBinaryCombo::dbQueryBinaryCombo(dbQueryBinaryCombo& rhs)
- {
- mLhs = rhs.mLhs;
- mRhs = rhs.mRhs;
- mComboOp = rhs.mComboOp;
- mTempKeyStoreToDispose = 0;
- // don't need to clear rhs pointers as not deleted
- }
-
-
- dbQueryBinaryCombo& dbQueryBinaryCombo::operator=(dbQueryBinaryCombo& rhs)
- {
- assert(0); // should never be called
- return *this;
- };
-
-
- dbQueryClause::QueryClauseTypes dbQueryBinaryCombo::queryClauseType() const
- {
- return binaryCombination;
- }
-
-
- const dbQueryClause* dbQueryBinaryCombo::item(unsigned int i) const
- {
- assert(i<2); // for now, later will have a list of items
- if (i==0)
- return mLhs;
- else
- return mRhs;
- }
-
-
- unsigned long dbQueryBinaryCombo::pairFieldsIfCouldUseCompoundIndex() const
- {
- if (mComboOp != and)
- return 0; // early failure - they're not ANDed
-
- if (mLhs->queryClauseType() != binaryfieldToLiteral)
- return 0;
-
- if (mRhs->queryClauseType() != binaryfieldToLiteral)
- return 0;
-
- dbQueryBinary* lhs = (dbQueryBinary*) mLhs; // safe downcast due to above tests
- if (lhs->binaryOperator() != equals)
- return 0;
-
- dbQueryBinary* rhs = (dbQueryBinary*) mRhs; // safe downcast due to above tests
- if (rhs->binaryOperator() != equals)
- return 0;
-
- // if get here have an expression that CAN be used with a compound index
- // ie: A==a && B==b
- const dbField* fld = lhs->lhsField();
- unsigned long ret = fld->fieldNumber();
- fld = rhs->lhsField();
- ret = (ret << 16) + fld->fieldNumber();
- return ret;
- }
-
-
- dbQueryBinary dbQueryBinaryCombo::makeCompoundSearch(const dbCompoundField* fld)
- {
- // this gets a little complicated:
- // we make this look like a string query, BUT
- // either of our components can really be binary values
- // and we also must pad out the LHS to its full key width
- assert(fld->fieldLen()<USHRT_MAX);
- unsigned short keyLen = fld->fieldLen();
- char* target = new char[keyLen]; // uh oh - we need to delete this after the query succeeds!
- assert(target);
- if (mTempKeyStoreToDispose)
- delete[] mTempKeyStoreToDispose; // just in case we are reusing a query object somehow
- mTempKeyStoreToDispose = target;
-
- {
- assert(mLhs->queryClauseType() == binaryfieldToLiteral);
- dbQueryLiteral* lit = ((dbQueryBinary*) mLhs)->literalClause(); // safe downcast
- assert(lit);
- memset(target, 0, keyLen); // clear the entire compound key width
- // NOT YET IMPLEMENTED - would be a loop for more than 2 segments
- memcpy(target, lit->binaryContents(), lit->literalLen());
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
- }
-
- assert(mRhs->queryClauseType() == binaryfieldToLiteral);
- dbQueryBinary* rhs = (dbQueryBinary*) mRhs; // safe downcast
- dbQueryLiteral* finalLit = rhs->literalClause();
- target += fld->segment(0)->fieldLen();
- memcpy(target, finalLit->binaryContents(), finalLit->literalLen());
- #ifdef OOF_SmartHeap
- MemPoolCheck(MemDefaultPool);
- #endif
-
- return dbQueryBinary( new dbQueryField(fld),
- rhs->binaryOperator(),
- new dbQueryLiteralBLOB(mTempKeyStoreToDispose, keyLen)
- );
- }
-
-
- // -------------------------------------------------------
- // d b Q u e r y T r i n a r y
- // -------------------------------------------------------
- // NOTE there is a lot of horrible code in this class
- // it is offensively coarse, but reasonably efficient and a long way
- // down the priority list for a rewrite.
- dbQueryTrinary::dbQueryTrinary(dbQueryTrinary& rhs)
- {
- mLhs = rhs.mLhs;
- mFrom = rhs.mFrom;
- mTo = rhs.mTo;
- mTrinOp = rhs.mTrinOp;
- rhs.mLhs = 0;
- rhs.mFrom = 0;
- rhs.mTo = 0;
- }
-
-
- dbQueryTrinary::~dbQueryTrinary()
- {
- delete mLhs;
- delete mFrom;
- delete mTo;
- }
-
-
- dbQueryTrinary& dbQueryTrinary::operator=(dbQueryTrinary& rhs)
- {
- assert(0); // should never be called
- return *this;
- };
-
-
- dbQueryClause::QueryClauseTypes dbQueryTrinary::queryClauseType() const
- {
- return trinaryFieldToLiterals;
- }
-
-
- dbQueryLiteralStr* dbQueryTrinary::literalStrFromClause() const
- {
- if (mFrom->queryClauseType()==atomicLiteralStr)
- return (dbQueryLiteralStr*) mFrom; // safe downcast
- else
- return 0;
- }
-
-
- dbQueryLiteralStr* dbQueryTrinary::literalStrToClause() const
- {
- if (mTo->queryClauseType()==atomicLiteralStr)
- return (dbQueryLiteralStr*) mTo; // safe downcast
- else
- return 0;
- }
-
-
- dbQueryLiteral* dbQueryTrinary::literalFromClause() const
- {
- if (mFrom->queryClauseType()==atomicLiteral)
- return (dbQueryLiteral*) mFrom; // safe downcast
- return 0;
- }
-
-
- dbQueryLiteral* dbQueryTrinary::literalToClause() const
- {
- if (mTo->queryClauseType()==atomicLiteral)
- return (dbQueryLiteral*) mTo; // safe downcast
- else
- return 0;
- }
-
-
- // -------------------------------------------------------
- // d b Q u e r y F i e l d
- // -------------------------------------------------------
- dbQueryClause::QueryClauseTypes dbQueryField::queryClauseType() const
- {
- return atomicfield;
- }
-
-
- // -------------------------------------------------------
- // d b Q u e r y L i t e r a l
- // -------------------------------------------------------
- dbQueryClause::QueryClauseTypes dbQueryLiteral::queryClauseType() const
- {
- return atomicLiteral;
- }
-
-
- const void* dbQueryLiteral:: binaryContents() const
- {
- return 0;
- }
-
-
- unsigned short dbQueryLiteral::literalLen() const
- {
- return 0;
- }
-
-
- // -------------------------------------------------------
- // d b Q u e r y L i t e r a l L o n g
- // -------------------------------------------------------
- const void* dbQueryLiteralLong:: binaryContents() const
- {
- return &mNum;
- }
-
-
- unsigned short dbQueryLiteralLong::literalLen() const
- {
- return sizeof(long);
- }
-
-
-
- // -------------------------------------------------------
- // d b Q u e r y L i t e r a l U l o n g
- // -------------------------------------------------------
- const void* dbQueryLiteralUlong:: binaryContents() const
- {
- return &mNum;
- }
-
-
- unsigned short dbQueryLiteralUlong::literalLen() const
- {
- return sizeof(unsigned long);
- }
-
-
-
- // -------------------------------------------------------
- // d b Q u e r y L i t e r a l S h o r t
- // -------------------------------------------------------
- const void* dbQueryLiteralShort:: binaryContents() const
- {
- return &mNum;
- }
-
-
- unsigned short dbQueryLiteralShort::literalLen() const
- {
- return sizeof(short);
- }
-
-
-
- // -------------------------------------------------------
- // d b Q u e r y L i t e r a l U s h o r t
- // -------------------------------------------------------
- const void* dbQueryLiteralUshort:: binaryContents() const
- {
- return &mNum;
- }
-
-
- unsigned short dbQueryLiteralUshort::literalLen() const
- {
- return sizeof(unsigned short);
- }
-
-
-
- // -------------------------------------------------------
- // d b Q u e r y L i t e r a l D o u b l e
- // -------------------------------------------------------
- const void* dbQueryLiteralDouble:: binaryContents() const
- {
- return &mNum;
- }
-
-
- unsigned short dbQueryLiteralDouble::literalLen() const
- {
- return sizeof(double);
- }
-
-
-
- // -------------------------------------------------------
- // d b Q u e r y L i t e r a l B L O B
- // -------------------------------------------------------
- const void* dbQueryLiteralBLOB:: binaryContents() const
- {
- return mBLOB;
- }
-
-
- unsigned short dbQueryLiteralBLOB::literalLen() const
- {
- return mBLOBlen;
- }
-
-
-
- // -------------------------------------------------------
- // d b Q u e r y L i t e r a l S t r
- // -------------------------------------------------------
- dbQueryClause::QueryClauseTypes dbQueryLiteralStr::queryClauseType() const
- {
- return atomicLiteralStr;
- }
-
-
- const void* dbQueryLiteralStr::binaryContents() const
- {
- return mStr;
- }
-
-
- unsigned short dbQueryLiteralStr::literalLen() const
- {
- if (!mStr)
- return 0;
- return strlen(mStr);
- }
-
-
-